<template>
  <div>
    <Searchbar>
      <el-autocomplete
        class="inline-input"
        v-model="selectedSchool"
        :fetch-suggestions="querySchool"
        placeholder="请输入学校名称"
        @select="selectSchool"
      ></el-autocomplete>
      <el-input @change="getList" v-model="search.college_name" placeholder="请输入学院名称"></el-input>

      <el-button class="search-btn" @click="getList" type="primary">查询</el-button>
    </Searchbar>

    <TableBar>
      <el-button @click="onShowCreate" type="primary" icon="el-icon-plus">新建</el-button>

      <el-button @click="onShowUpload" icon="el-icon-upload">批量上传</el-button>
    </TableBar>

    <el-table :data="tableData" style="width: 100%">
      <el-table-column fixed="left" type="selection" width="55"></el-table-column>
      <el-table-column fixed="left" type="index" width="55">序号</el-table-column>
      <el-table-column prop="college_id" label="学院id"></el-table-column>
      <el-table-column prop="school_name" label="学校名称"></el-table-column>
      <el-table-column prop="college_name" label="学院名称"></el-table-column>
      <el-table-column prop="create_time" label="创建时间"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button @click="onShowEdit(scope.row)" size="small" class="text-default">编辑</el-button>
          <el-button
            v-if="scope.row.status == 'N'"
            type="text"
            @click="onDel(scope.row)"
            size="small"
            class="text-warning"
          >停用</el-button>
          <el-button
            v-else
            type="text"
            @click="onDel(scope.row)"
            size="small"
            class="text-success"
          >启用</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change=handleSizeChange
      :current-page.sync="search.page"
      :page-size="search.page_size"
      :total="total"
    ></el-pagination>
    <EditCollege @onClose="onHideEdit" @afterSubmit="getList" :obj="editRow" :show="showEdit" />

    <MulUpload
      action="/object/batch/create/"
      title="导入学院"
      temp="10"
      type="5"
      @onClose="onHideUpload"
      :show="showUpload"
    />
  </div>
</template>
<script>
import EditCollege from "@/modals/EditCollege";
import MulUpload from "@/components/MulUpload";
import CreateFilter from "@/mixins/CreateFilter";
import showEditMixin from "@/mixins/showEditMixin";
import uploadMixin from "@/mixins/uploadMixin";
import pageMixin from "@/mixins/pageMixin";
import { collegeModel } from "@/api/admin_models";
import Searchbar from "@/components/Searchbar";
import TableBar from "@/components/TableBar";
import { Message } from "element-ui";
export default {
  mixins: [CreateFilter, showEditMixin, uploadMixin, pageMixin],
  components: { EditCollege, MulUpload, Searchbar, TableBar },
  data() {
    return {
      tableData: [],
      total: 0,
      search: {
        selectedschool_id: null,
        college_name: null,
        page: 1,
      },
    };
  },
  mounted() {
    this.getList();
  },
  methods: {
    getList() {
      console.log("getList");
      collegeModel.objectList(
        this.search,
        (res) => {
          this.total = res.total_records;
          this.tableData = res.data.map((v) => {
            return v;
          });
        },
        (err) => {
          console.log(err);
        }
      );
    },
    onDel(row) {
      let action = row.status == "N" ? "停用" : "启用";
      this.$confirm(`确定${action}${row.college_name}`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          collegeModel.update(
            row.college_id,
            {
              status: row.status == "N" ? "C" : "N",
            },
            (res) => {
              Message.success(res.message);
              this.getList();
            },
            (err) => {
              Message.success(err.message);
            }
          );
        })
        .catch((err) => {
          console.error(err);
          this.$message({
            type: "info",
            message: "已取消操作",
          });
        });
    },
  },
};
</script>
<style lang="scss">
</style>